﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using System.Xml;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Serialization;

namespace JYRWPH
{
    public partial class MainPage : PhoneApplicationPage
    {
        private string xmlPathList = "db/index.xml";
        private string xmlPathRecord = "db/record.xml";

        List<Record> listRecord = new List<Record>();

        // 构造函数
        public MainPage()
        {
            InitializeComponent();

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            //获取列表
            XElement xDoc = XElement.Load(xmlPathList);
            XElement xDocRecord = XElement.Load(xmlPathRecord);
            XElement xC = xDoc.Element("channel");
            //List<Record> listRecord = new List<Record>();

            listRecord = GetRecordList();

            ObservableCollection<PersonList> oP = new ObservableCollection<PersonList>();

            foreach (XElement item in xC.Elements("item"))
            {
                //XElement ep = (from ps in xDocRecord.Element("root").Elements("person")
                //               where ps.Attribute("title").Value == item.Element("title").Value.ToString()
                //               select ps) as XElement;


                var re = from r in listRecord
                         where r.Title == item.Element("title").Value.ToString()
                         select r;


                PersonList pl = new PersonList()
                {
                    Title = item.Element("title").Value.ToString(),
                    Link = item.Element("link").Value.ToString(),
                    Date = (re.Count() == 0 ? "未读" : re.Single().Date)
                };
                oP.Add(pl);
            }

            pList.ItemsSource = oP;

        }

        private List<Record> GetRecordList()
        {
            List<Record> list = new List<Record>();
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists("record.xml"))
                {
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("record.xml", FileMode.Open))
                    {
                        if (stream.Length > 0)
                        {
                            XmlSerializer serializer = new XmlSerializer(typeof(List<Record>));
                            list = (List<Record>)serializer.Deserialize(stream);
                        }
                    }
                }
            }
            return list;
        }

        private void pList_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            PersonList pl = pList.SelectedItem as PersonList;

            if ((listRecord = GetRecordList()).Count > 0)
            {
                var p1 = from p11 in listRecord
                         where p11.Title == pl.Title
                         select p11;
                if (p1.Count() > 0)
                    listRecord.Remove(p1.Single());
            }

            using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Record>));

                using (IsolatedStorageFileStream isoStream = iso.OpenFile("record.xml", FileMode.Create))
                {
                    listRecord.Add(new Record() { Title = pl.Title, Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") });

                    XmlWriterSettings xmlsetting = new XmlWriterSettings();
                    xmlsetting.Indent = true;

                    using (XmlWriter xmlWriter = XmlWriter.Create(isoStream, xmlsetting))
                    {
                        serializer.Serialize(xmlWriter, listRecord);
                    }
                }
            }

            NavigationService.Navigate(new Uri(String.Format("/Detail.xaml?link={0}", pl.Link), UriKind.Relative));
        }
    }


}